package com.lin.excel.service.impl;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.alibaba.excel.EasyExcel;
import com.lin.excel.dao.ExcelDao;
import com.lin.excel.service.ExcelService;
import lombok.val;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ExcelServiceImpl
 * @Description
 * @Author lin
 * @Date 2022/7/21 下午11:42
 * @Version V1.0
 */
@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private ExcelDao excelDao;

    @Override
    public void exportUser(String name, HttpServletResponse response) {
        List<UserVO> users = excelDao.findUsersByName(name);
        String[] header = {"姓名", "性别", "编号","住址","入职hr","入职时间","是否离职"};

        //HSSFWorkbook workbook = new HSSFWorkbook();
        //Workbook workbook = new XSSFWorkbook();
        Workbook workbook = new SXSSFWorkbook();
        // 生成一个表格，设置表格名称
        Sheet sheet = workbook.createSheet("导出结果表");
        // 设置表格列宽度为12个字节
        sheet.setDefaultColumnWidth(12);
        // 创建标题的显示样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 创建第一行表头
        Row headrow = sheet.createRow(0);
        // 遍历添加表头
        for (int i = 0; i < header.length; i++) {
            // 创建一个单元格
            Cell cell = headrow.createCell(i);
            // 创建一个内容对象
            RichTextString text = new XSSFRichTextString(header[i]);
            // 将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
            cell.setCellStyle(headerStyle);
        }
        for (int i = 0; i < users .size(); i++) {
            // 创建一行
            Row row1 = sheet.createRow(i + 1);
            // 第一列创建并赋值
            row1.createCell(0).setCellValue(new XSSFRichTextString(users.get(i).getName()));
            // 第二列创建并赋值
            row1.createCell(1).setCellValue(new XSSFRichTextString(users.get(i).getGender()));
            // 第三列创建并赋值
            row1.createCell(2).setCellValue(new XSSFRichTextString(String.valueOf(users.get(i).getNumber())));
            // 第4列创建并赋值
            row1.createCell(3).setCellValue(new XSSFRichTextString(users.get(i).getAddress()));
            // 第5列创建并赋值
            row1.createCell(4).setCellValue(new XSSFRichTextString(users.get(i).getCreatedBy()));
            // 第4列创建并赋值
            row1.createCell(5).setCellValue(new XSSFRichTextString(users.get(i).getCreatedDate().toString()));
            // 第4列创建并赋值
            row1.createCell(6).setCellValue(new XSSFRichTextString(users.get(i).getDelFlag()));
        }
        response.setContentType("application/octet-stream");
        // 设置导出Excel的名称
        response.setHeader("Content-disposition", "attachment;filename=result.xlsx");
        // 刷新缓冲
        try {
            response.flushBuffer();
            // workbook将Excel写入到response的输出流中，供页面下载
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void easyPoiExportUser(String name,HttpServletRequest request, HttpServletResponse response) {
        List<EasyPoiUserDO> users = excelDao.findEasyPoiUsersByName(name);
        ExportParams exportParams = new ExportParams("用户列表", "会员列表", ExcelType.XSSF);
        ModelMap map = new ModelMap();
        map.put(NormalExcelConstants.DATA_LIST, users);
        map.put(NormalExcelConstants.CLASS, EasyPoiUserDO.class);
        map.put(NormalExcelConstants.PARAMS, exportParams);
        map.put(NormalExcelConstants.FILE_NAME, "用户信息");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    @Override
    public void easyExcelExportUser(String name,HttpServletRequest request, HttpServletResponse response) {
        try{
            String fileName = "userList";
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            List<EasyExcelUserDO> list = excelDao.findEasyExcelUsersByName(name);
            EasyExcel.write(response.getOutputStream(), EasyExcelUserDO.class).sheet("模板").doWrite(list);
        }catch (IOException e) {

        }

    }
}
